package com.mapapp.flutter_mapapp.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mapapp.flutter_mapapp.R;
import com.mapapp.flutter_mapapp.bean.StepFloorBean;
import com.mapapp.flutter_mapapp.widget.CustomizedProgressBar;

public class StepListDialogAdapter extends RecyclerView.Adapter<StepListDialogAdapter.StepListViewHolder>{

    private StepFloorBean mSteps;
    private Context mContext;
    private int mPosition = 0;

    public StepListDialogAdapter(StepFloorBean steps , Context content){
        this.mSteps = steps;
        this.mContext = content;
    }

    public void notify(StepFloorBean steps){
        this.mSteps = steps;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public StepListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_step_list, parent, false);
        StepListViewHolder holder = new StepListViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull StepListViewHolder holder, final int position) {
        String[] colors = mContext.getResources().getStringArray(R.array.stepcolor);
        StepFloorBean bean = mSteps.getRegions().get(position);
        holder.houseNum.setText(bean.getRegionName());
        holder.houseStep.setText(bean.getProcessName());
        if(bean.getDbFlag() == 1){
            holder.finish.setVisibility(View.VISIBLE);
            holder.finish.setText("进行中");
            holder.llStep.setVisibility(View.GONE);
        }else{
            if(bean.getProcessPercent() == 1){
                holder.finish.setVisibility(View.VISIBLE);
                holder.llStep.setVisibility(View.GONE);
            }else{
                holder.finish.setVisibility(View.GONE);
                if("".equals(bean.getProcessId())||"".equals(bean.getProcessName())){
                    holder.llStep.setVisibility(View.GONE);
                }else{
                    holder.llStep.setVisibility(View.VISIBLE);
                    for(int i = 0 ; i < bean.getProcesses().size() ; i ++){
                        if(bean.getProcessId().equals(bean.getProcesses().get(i).getId())){
                            switch (i){
                                case 0:
                                    holder.bar.setGradualColor(Color.parseColor(colors[0]));
                                    break;
                                case 1:
                                    holder.bar.setGradualColor(Color.parseColor(colors[1]));
                                    break;
                                case 2:
                                    holder.bar.setGradualColor(Color.parseColor(colors[2]));
                                    break;
                                case 3:
                                    holder.bar.setGradualColor(Color.parseColor(colors[3]));
                                    break;
                                case 4:
                                    holder.bar.setGradualColor(Color.parseColor(colors[4]));
                                    break;
                                default:
                                    holder.bar.setGradualColor(Color.parseColor(colors[5]));
                                    break;
                            }
                        }
                    }
                    holder.bar.setCurrentCount(100);
                }
            }
        }

        holder.llHouse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPosition = position;
                notifyDataSetChanged();
                if(onItemClickListener != null){
                    onItemClickListener.onItemClick(position);
                }
            }
        });
        if(mPosition == position){
            holder.llHouse.setBackgroundResource(R.mipmap.icon_house_xz);
        }else{
            holder.llHouse.setBackgroundResource(R.mipmap.icon_house);
        }
    }

    @Override
    public int getItemCount() {
        if (mSteps == null) {
            return 0;
        }
        return mSteps.getRegions().size();
    }

    static class StepListViewHolder extends RecyclerView.ViewHolder {
        private TextView houseNum;
        private TextView houseStep;
        private CustomizedProgressBar bar;
        private TextView finish;
        private LinearLayout llStep;
        private LinearLayout llHouse;

        public StepListViewHolder(View itemView) {
            super(itemView);
            houseNum = itemView.findViewById(R.id.tv_step_list_house_num);
            houseStep = itemView.findViewById(R.id.tv_step_list_house_step);
            bar = itemView.findViewById(R.id.cpb_item_step_list_house_process);
            finish = itemView.findViewById(R.id.tv_item_step_list_finish);
            llStep = itemView.findViewById(R.id.ll_item_step_list_step);
            llHouse = itemView.findViewById(R.id.ll_item_step_house);
        }
    }

    //私有属性
    private OnItemClickListener onItemClickListener = null;

    //setter方法
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    //回调接口
    public interface OnItemClickListener {
        void onItemClick(int position);
    }
}
